home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-20 / nos_kit3.zip / UUDCD.C < prev    next >
C/C++ Source or Header  |  1990-06-08  |  3KB  |  197 lines

  1. #define MSDOS
  2. /* uudecode.c */
  3.  
  4. #ifndef lint
  5. static char sccsid[] = "@(#)uudecode.c    5.1 (Berkeley) 7/2/83";
  6. #endif
  7.  
  8. /*
  9.  * uudecode [input]
  10.  *
  11.  * create the specified file, decoding as you go.
  12.  * used with uuencode.
  13.  */
  14. #include <stdio.h>
  15. #ifndef MSDOS
  16. #include <pwd.h>
  17. #endif
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20.  
  21. /* single character decode */
  22. #define DEC(c)    (((c) - ' ') & 077)
  23.  
  24. main(argc, argv)
  25. char **argv;
  26. {
  27.     FILE *in, *out;
  28.     struct stat sbuf;
  29.     int mode;
  30.     char dest[128];
  31.     char buf[80];
  32.  
  33.     /* optional input arg */
  34.     if (argc > 1) {
  35.         if ((in = fopen(argv[1], "r")) == NULL) {
  36.             perror(argv[1]);
  37.             exit(1);
  38.         }
  39.         argv++; argc--;
  40.     } else
  41.         in = stdin;
  42.  
  43.     if (argc != 1) {
  44.         printf("Usage: uudecode [infile]\n");
  45.         exit(2);
  46.     }
  47.  
  48.     /* search for header line */
  49.     for (;;) {
  50.         if (fgets(buf, sizeof buf, in) == NULL) {
  51.             fprintf(stderr, "No begin line\n");
  52.             exit(3);
  53.         }
  54.         if (strncmp(buf, "begin ", 6) == 0)
  55.             break;
  56.     }
  57.     sscanf(buf, "begin %o %s", &mode, dest);
  58.  
  59.     /* handle ~user/file format */
  60. #ifndef MSDOS
  61.     if (dest[0] == '~') {
  62.         char *sl;
  63.         struct passwd *getpwnam();
  64.         char *index();
  65.         struct passwd *user;
  66.         char dnbuf[100];
  67.  
  68.         sl = index(dest, '/');
  69.         if (sl == NULL) {
  70.             fprintf(stderr, "Illegal ~user\n");
  71.             exit(3);
  72.         }
  73.         *sl++ = 0;
  74.         user = getpwnam(dest+1);
  75.         if (user == NULL) {
  76.             fprintf(stderr, "No such user as %s\n", dest);
  77.             exit(4);
  78.         }
  79.         strcpy(dnbuf, user->pw_dir);
  80.         strcat(dnbuf, "/");
  81.         strcat(dnbuf, sl);
  82.         strcpy(dest, dnbuf);
  83.     }
  84. #endif
  85.  
  86.     /* create output file */
  87. #ifdef MSDOS
  88.     /* binary output file */
  89.     out = fopen(dest, "wb");
  90. #else
  91.     out = fopen(dest, "w");
  92. #endif
  93.     if (out == NULL) {
  94.         perror(dest);
  95.         exit(4);
  96.     }
  97.     chmod(dest, mode);
  98.  
  99.     decode(in, out);
  100.  
  101.     if (fgets(buf, sizeof buf, in) == NULL || strcmp(buf, "end\n")) {
  102.         fprintf(stderr, "No end line\n");
  103.         exit(5);
  104.     }
  105.     exit(0);
  106. }
  107.  
  108. /*
  109.  * copy from in to out, decoding as you go along.
  110.  */
  111. decode(in, out)
  112. FILE *in;
  113. FILE *out;
  114. {
  115.     char buf[80];
  116.     char *bp;
  117.     int n;
  118.  
  119.     for (;;) {
  120.         /* for each input line */
  121.         if (fgets(buf, sizeof buf, in) == NULL) {
  122.             printf("Short file\n");
  123.             exit(10);
  124.         }
  125.         n = DEC(buf[0]);
  126.         if (n <= 0)
  127.             break;
  128.  
  129.         bp = &buf[1];
  130.         while (n > 0) {
  131.             outdec(bp, out, n);
  132.             bp += 4;
  133.             n -= 3;
  134.         }
  135.     }
  136. }
  137.  
  138. /*
  139.  * output a group of 3 bytes (4 input characters).
  140.  * the input chars are pointed to by p, they are to
  141.  * be output to file f.  n is used to tell us not to
  142.  * output all of them at the end of the file.
  143.  */
  144. outdec(p, f, n)
  145. char *p;
  146. FILE *f;
  147. {
  148.     int c1, c2, c3;
  149.  
  150.     c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
  151.     c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
  152.     c3 = DEC(p[2]) << 6 | DEC(p[3]);
  153.     if (n >= 1)
  154.         putc(c1, f);
  155.     if (n >= 2)
  156.         putc(c2, f);
  157.     if (n >= 3)
  158.         putc(c3, f);
  159. }
  160.  
  161.  
  162. /* fr: like read but stdio */
  163. int
  164. fr(fd, buf, cnt)
  165. FILE *fd;
  166. char *buf;
  167. int cnt;
  168. {
  169.     int c, i;
  170.  
  171.     for (i=0; i<cnt; i++) {
  172.         c = getc(fd);
  173.         if (c == EOF)
  174.             return(i);
  175.         buf[i] = c;
  176.     }
  177.     return (cnt);
  178. }
  179.  
  180. /*
  181.  * Return the ptr in sp at which the character c appears;
  182.  * NULL if not found
  183.  */
  184.  
  185. #define    NULL    0
  186.  
  187. char *
  188. index(sp, c)
  189. register char *sp, c;
  190. {
  191.     do {
  192.         if (*sp == c)
  193.             return(sp);
  194.     } while (*sp++);
  195.     return(NULL);
  196. }
  197.